package com.lingxinst.farm.app.ui

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager2.widget.ViewPager2
import com.lingxinst.farm.app.entity.IndexBannerEntity
import com.lingxinst.farm.app.entity.IndexEntity
import com.lingxinst.farm.app.utils.PageKey
import com.lingxinst.farm.app.utils.PageUtils
import com.lingxinst.farm.app.R
import com.lingxinst.farm.app.databinding.ActivityIndexBinding
import com.lingxinst.farm.app.databinding.ItemIndexPageBinding
import com.lynn.common.utils.ScreenUtil
import com.lingxinst.farm.base.ui.BaseMvvmActivity
import com.lingxinst.farm.base.ui.BaseSingleAdapter
import com.lingxinst.farm.base.utils.GlideUtils
import com.lingxinst.farm.base.vm.BaseViewModel
import kotlinx.serialization.json.Json

/**
 * @description: 引导页，app首次启动时才会显示
 * @author: Lynn
 * @datetime: 2025/6/10 11:55
 **/
class IndexActivity :
    BaseMvvmActivity<ActivityIndexBinding, IndexViewModel>(R.layout.activity_index) {

    private var _indexEntity: IndexEntity? = null
    val indexEntity : IndexEntity? get() = _indexEntity

    private lateinit var _urlList: List<IndexBannerEntity>

    private val pointViewList: MutableList<ImageView> = mutableListOf()
    private var curPageIndex = 0

    override fun initTag(): String {
        return this.javaClass.simpleName
    }


    override fun getVMClass(): Class<IndexViewModel> {
        return IndexViewModel::class.java
    }

    override fun setBindingData() {

    }

    override fun initUIView(savedInstanceState: Bundle?) {
        // 获取图片列表数据
        val json = intent.getStringExtra(PageKey.INDEX_ENTITY) ?: "{}"
        _indexEntity = Json.decodeFromString<IndexEntity>(json)
        _urlList = _indexEntity?.bannerList ?: listOf()

        // 根据图片数量添加指示器
        addPointView(_urlList.size)

        // 设置ViewPager的适配器
        val adapter = Adapter(this)
        adapter.setDataList(_urlList)
        mBinding.vpIndex.adapter = adapter

        setListener()

    }


    /**
     * 根据图片数量添加指示器
     */
    private fun addPointView(size: Int) {
        mBinding.pointContainer.removeAllViews()
        pointViewList.clear()
        for (i in 0 until size) {
            val iv = ImageView(this)
            iv.setBackgroundResource(R.drawable.ic_index_banner)
            iv.isSelected = i == 0

            val wh = ScreenUtil.dpUnit(this, 8f).toInt()
            val llp = LinearLayout.LayoutParams(wh, wh)

            val margin = ScreenUtil.dpUnit(this, 5f).toInt()
            llp.setMargins(margin, margin, margin, margin)
            iv.layoutParams = llp

            mBinding.pointContainer.addView(iv)
            pointViewList.add(iv)
        }
    }

    private fun setListener() {
        mBinding.vpIndex.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                if (position < 0 || position > pointViewList.size - 1 || curPageIndex == position) {
                    return
                }

                pointViewList[curPageIndex].isSelected = false
                pointViewList[position].isSelected = true
                curPageIndex = position
            }
        })
    }



}



private class Adapter(val activity: IndexActivity) :
    BaseSingleAdapter<IndexBannerEntity, ItemIndexPageBinding>() {
    override fun getItemLayoutId(): Int {
        return R.layout.item_index_page
    }

    override fun bindData(binding: ItemIndexPageBinding, t: IndexBannerEntity, position: Int) {
        binding.btnEnter.visibility = when (position) {
            itemCount - 1 -> View.VISIBLE
            else -> View.GONE
        }
        binding.btnEnter.setOnClickListener {
//            PageUtils.jumpMain(activity,activity.indexEntity?.appVersion)
        }
        GlideUtils.into(activity, t.coverUrl, binding.iv)
    }
}


class IndexViewModel : BaseViewModel() {

}